package com.netease.content_sales.controller;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.netease.content_sales.mate.BuyList;
import com.netease.content_sales.mate.Product;
import com.netease.content_sales.mate.User;
import com.netease.content_sales.service.IProductService;

@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	@Autowired
	private IProductService productService;

	/**
	 * @Title: index
	 * @Description: TODO(3展示页)
	 * @param session
	 * @param locale
	 * @param model
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(HttpSession session, Locale locale, Model model) {
		logger.info("/! The client locale is {}.", locale);
		User user = (User) session.getAttribute("user");
		Number userId = null;
		if (user != null)
		{
			model.addAttribute("user", user);
			userId = user.getId();
		}
		else
		{
			System.out.println("no login");
		}
		List<Product> productList = productService.getProductList(userId);
		model.addAttribute("productList", productList);
		return "index";
	}

	/**
	 * @Title: show
	 * @Description: TODO(4查看页)
	 * @param session
	 * @param id
	 * @param locale
	 * @param model
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String show(HttpSession session, @RequestParam(value = "id", required = true) Number id, Locale locale, Model model) {
		logger.info("show! The client locale is {}.", locale);
		User user = (User) session.getAttribute("user");
		Number userId = null;
		if (user != null)
		{
			model.addAttribute("user", user);
			userId = user.getId();
		}
		else
		{
			System.out.println("no login");
		}
		System.out.println("id:"+id+"userId:"+userId);
		Product product = productService.getProductDetail(id, userId);
		model.addAttribute("product", product);
		return "show";
	}

	/**
	 * @Title: account
	 * @Description: TODO(5账务页)
	 * @param session
	 * @param locale
	 * @param model
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/account", method = RequestMethod.GET)
	public String account(HttpSession session, Locale locale, Model model) {
		logger.info("account! The client locale is {}.", locale);
		User user = (User) session.getAttribute("user");
		Number userId = null;
		if (user != null)
		{
			model.addAttribute("user", user);
			userId = user.getId();
		}
		else
		{
			System.out.println("no login");
			return "redirect:login";
		}
		List<BuyList> buyList = productService.getBuyList(userId);
		model.addAttribute("buyList", buyList);
		return "account";
	}

	/**
	 * @Title: publicPage
	 * @Description: TODO(6发布页)
	 * @param session
	 * @param locale
	 * @param model
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/public", method = RequestMethod.GET)
	public String publicPage(HttpSession session, Locale locale, Model model) {
		logger.info("publicPage! The client locale is {}.", locale);
		User user = (User) session.getAttribute("user");
		if (user != null)
		{
			model.addAttribute("user", user);
		}
		else
		{
			System.out.println("no login");
			return "redirect:login";
		}
		return "public";
	}

	/**
	 * @Title: publicPage
	 * @Description: TODO(7发布提交)
	 * @param session
	 * @param locale
	 * @param model
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/publicSubmit", method = RequestMethod.POST)
	public String publicSubmit(HttpSession session, @RequestParam(value = "title", required = true) String title, @RequestParam(value = "image", required = true) String image,
			@RequestParam(value = "detail", required = true) String detail, @RequestParam(value = "price", required = true) Number price,
			@RequestParam(value = "summary", required = true) String summary, Locale locale, Model model) {
		logger.info("publicSubmit! The client locale is {}.", locale);
		User user = (User) session.getAttribute("user");
		Number userId = null;
		if (user != null)
		{
			model.addAttribute("user", user);
			userId = user.getId();
		}
		else
		{
			System.out.println("no login");
			return "redirect:login";
		}
		Product product = new Product();
		product.setDetail(detail);
		product.setImage(image);
		product.setPrice(Double.valueOf(price.toString()));
		product.setSummary(summary);
		product.setTitle(title);
		product.setSeller((Long) userId);
		Integer result = productService.insertProduct(product);
		if (result > 0)
		{
			System.out.println(result);
			model.addAttribute("product", product);
		}
		return "publicSubmit";
	}

	/**
	 * @Title: edit
	 * @Description: TODO(8编辑页)
	 * @param session
	 * @param id
	 * @param locale
	 * @param model
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(HttpSession session, @RequestParam(value = "id", required = true) Number id, Locale locale, Model model) {
		logger.info("edit! The client locale is {}.", locale);
		User user = (User) session.getAttribute("user");
		Number userId = null;
		if (user != null)
		{
			model.addAttribute("user", user);
			userId = user.getId();
		}else
		{
			System.out.println("no login");
			return "redirect:login";
		}
		Product product = productService.getProductDetail(id, userId);
		model.addAttribute("product", product);
		return "edit";
	}

	/**
	 * @Title: editSubmit
	 * @Description: TODO(9编辑提交)
	 * @param session
	 * @param id
	 * @param locale
	 * @param model
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/editSubmit", method = RequestMethod.POST)
	public String editSubmit(HttpSession session, @RequestParam(value = "id", required = true) Number id, @RequestParam(value = "title", required = true) String title,
			@RequestParam(value = "image", required = true) String image, @RequestParam(value = "detail", required = true) String detail, @RequestParam(value = "price", required = true) Number price,
			@RequestParam(value = "summary", required = true) String summary, Locale locale, Model model) {
		logger.info("editSubmit! The client locale is {}.", locale);
		User user = (User) session.getAttribute("user");
		Number userId = null;
		if (user != null)
		{
			model.addAttribute("user", user);
			userId = user.getId();
		}else
		{
			System.out.println("no login");
			return "redirect:login";
		}
		// 获取历史数据,没有则直接返回
		Product product = productService.getProductDetail(id, userId);
		if (product == null)
		{
			return "editSubmit";
		}
		// 替换新内容
		product.setDetail(detail);
		product.setImage(image);
		product.setPrice(Double.valueOf(price.toString()));
		product.setSummary(summary);
		product.setTitle(title);
		// 更新数据库内容
		Integer result = productService.updateProduct(product);
		model.addAttribute("product", product);
		// 成功则返回数据
		if (result > 0)
		{
			System.out.println(result);
			model.addAttribute("product", product);
		}
		return "editSubmit";
	}
}
